import time
import jwt
import datetime
from python_work.flask_dome.app.config.Config import *
def generate_jwt(data):
    payload = {
        'user_id': data["id"],
        'data': data,
        'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=1)  # 设置过期时间为 1 小时后
    }
    return jwt.encode(payload, secret_key, algorithm='HS256')

def parse_jwt(jwt_token):
    try:
        _payload = jwt.decode(jwt_token, secret_key, algorithms=['HS256'])
    except jwt.PyJWTError:
        print('token解析失败')
        return False, None
    else:
        print(_payload)
        exp = int(_payload["exp"])
        if time.time() > exp:
            print('已失效')
            return False, None

    return True, _payload

if __name__ == '__main__':
    token = generate_jwt("gwl", "gwl", "gwl")
    print(token)
    l = parse_jwt("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiZ3dsIiwidXNlcm5hbWUiOiJnd2wiLCJleHAiOjE3Mjg4MzYzODh9.5er1tpViWFRMhs7_DKRrff5Bb2xfBO5QHGCGPYrsKI0", "gwl")
    print(l)